package com.zingrow.web.report.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zingrow.common.utils.MongoHelper;
import com.zingrow.web.base.service.impl.BaseService;
import com.zingrow.web.entity.InformationDistribute;
import com.zingrow.web.report.base.Channel;
import com.zingrow.web.report.base.CountNum;
import com.zingrow.web.report.base.Cycle;
import com.zingrow.web.report.base.TableField;
import com.zingrow.web.report.base.TextContants;
import com.zingrow.web.report.mapper.DimensionReckonMapper;
import com.zingrow.web.report.response.BaseData;
import com.zingrow.web.report.response.CountData;
import com.zingrow.web.report.response.ModuleData;
import com.zingrow.web.report.response.PlanItem;
import com.zingrow.web.report.response.RankData;
import com.zingrow.web.report.service.IRptDisplayService;
import com.zingrow.web.report.utils.ReportUtils;

@Service
public class RptDisplayServiceImpl extends BaseService<ModuleData> implements IRptDisplayService {
    @Autowired
    DimensionReckonMapper reckonMapper;

    @Override
    public void initMapper() {

    }

    @Override
    public void analyzeDisplayData(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        switch (moduleData.getId()) {
        case 1:
            // 新闻舆情概述
            summaryNews(moduleData, plans, groupName, dates);
            break;
        case 2:
            // 舆情走势
            trend(moduleData, plans, groupName, dates);
            break;
        case 3:
            // 舆情份额
            quotient(moduleData, plans, groupName, dates);
            break;
        case 4:
            // 总体敏感信息占比
            sensitive(moduleData, plans, groupName, dates);
            break;
        case 5:
            // 新闻敏感信息占比
            sensitiveNews(moduleData, plans, groupName, dates);
            break;
        case 6:
            // 媒体发稿top10
            mediaTop10(moduleData, plans, groupName, dates);
            break;
        case 7:
            // 新闻排行
            rankNews(moduleData, plans, groupName, dates);
            break;
        case 8:
            // 舆情信息对比
            compareOpinion(moduleData, plans, groupName, dates);
            break;
        case 9:
            // 一周数据总览
            weekDataT(moduleData, plans, groupName, dates);
            break;
        case 10:
            // 一周数据总览表
            weekDataB(moduleData, plans, groupName, dates);
            break;
        case 11:
            // 舆情走势-新闻
            trendNews(moduleData, plans, groupName, dates);
            break;
        case 12:
            // 自媒体敏感信息对比
            sensitiveMedias(moduleData, plans, groupName, dates);
            break;
        case 13:
            // 舆情概述
            summary(moduleData, plans, groupName, dates);
            break;
        case 14:
            // 敏感信息列表
            sensitiveInfoRank(moduleData, plans, groupName, dates);
            break;
        case 15:
            // 新闻数据
            dataNews(moduleData, plans, groupName, dates);
            break;
        case 16:
            // 新闻数据明细
            dataNewsDetail(moduleData, plans, groupName, dates);
            break;
        case 17:
            // 微信数据
        	dataWeixin(moduleData, plans, groupName, dates);
            break;
        case 18:
            // 微博数据
            dataWeibo(moduleData, plans, groupName, dates);
            break;
        case 19:
            // 论坛数据
            dataBbs(moduleData, plans, groupName, dates);
            break;
        case 20:
            break;
        case 21:
            break;
        case 22:
            break;
        case 23:
            break;

        default:
            break;
        }
    }

    /**
     * 舆情份额-3
     */
    private void quotient(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        // 统计新闻、论坛、微信、微博
        Channel[] channels = new Channel[] { Channel.NEWS, Channel.BBS, Channel.WEIXIN, Channel.WEIBO };
        boolean isNull = true;
        for (Channel channel : channels) {
            // 计算维度表的ID
            List<Long> ids = ReportUtils.buildIdsODay(dates, plans, channel.getIndex());
            // 从维度表中根据ID统计出数据
            Map<String, Object> map = new HashMap<>();
            map.put("ids", ids);
            map.put("group", 0);
            map.put("num", CountNum.PUBLISH.getIndex());
            map.put("types", new int[] { channel.getIndex() });
            List<CountData> datas = reckonMapper.reckonChannelNum(map);
            // 统计出任意数据时代表数据不为空
            if (datasNotEmpty(datas)) {
                isNull = false;
            }
            // 加入数据到返回的列表中
            moduleData.addDataList(channel.getName(), datas);
        }
        // 数据为空时清除已加入的无效数据列表
        if (isNull) {
            moduleData.setText("");
            moduleData.clearDataList();
        }
    }

    /**
     * 舆情走势-2
     */
    private void trend(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        // 统计新闻、论坛、微信、微博
        Channel[] channels = new Channel[] { Channel.NEWS, Channel.BBS, Channel.WEIXIN, Channel.WEIBO };
        boolean isNull = true;
        for (Channel channel : channels) {
            // 计算维度表的ID
            List<Long> ids = ReportUtils.buildIdsODay(dates, plans, channel.getIndex());
            // 从维度表中根据ID统计出数据
            Map<String, Object> map = new HashMap<>();
            map.put("ids", ids);
            map.put("num", CountNum.PUBLISH.getIndex());
            map.put("types", new int[] { channel.getIndex() });
            map.put("group", 1);
            List<CountData> datas = reckonMapper.reckonChannelNum(map);
            // 统计出任意数据时代表数据不为空
            if (datasNotEmpty(datas)) {
                isNull = false;
            }
            // 判断未统计的天数
            setNumZero(datas, dates, 1);
            // 按照时间排序
            Collections.sort(datas);
            // 加入数据到返回的列表中
            moduleData.addDataList(channel.getName(), datas);
        }
        // 数据为空时清除已加入的无效数据列表
        if (isNull) {
            moduleData.setText("");
            moduleData.clearDataList();
        }
    }

    /**
     * 舆情走势-新闻-11
     */
    private void trendNews(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        List<Long> ids = ReportUtils.buildIdsODay(dates, plans, Channel.NEWS.getIndex());
        // 查询原创数和转载量
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("num", CountNum.ORIGINAL_REPRINT.getIndex());
        map.put("types", new int[] { Channel.NEWS.getIndex() });
        map.put("group", 1);
        List<CountData> datas1 = reckonMapper.reckonChannelNum(map);
        if (datasNotEmpty(datas1)) {// 克隆一个数组储存转载量
            List<CountData> datas2 = ReportUtils.deepCopyList(datas1);
            // 若查询的数据为空，则根据日期补齐list并设置数量为0
            setNumZero(datas1, dates, 1);
            setNumZero(datas2, dates, 1);
            // 下面是为了保障返回的数据只有原创数或者转载量，设置另外一个为Null
            for (CountData countData : datas1) {
                countData.setSecondNum(null);
            }
            for (CountData countData : datas2) {
                countData.setFirstNum(countData.getSecondNum());
                countData.setSecondNum(null);
            }
            // 按照时间排序
            Collections.sort(datas1);
            // 按照时间排序
            Collections.sort(datas2);
            moduleData.addDataList("转载量", datas2);
            moduleData.addDataList("原创新闻", datas1);
        }

    }

    /**
     * 舆情概述-13
     */
    private void summary(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        Channel[] channels = new Channel[] { Channel.NEWS, Channel.BBS, Channel.WEIXIN, Channel.WEIBO };
        boolean isNull = true;
        String text = moduleData.getText();
        for (Channel channel : channels) {
            List<Long> ids = ReportUtils.buildIdsODay(dates, plans, channel.getIndex());
            Map<String, Object> map = new HashMap<>();
            map.put("ids", ids);
            map.put("num", CountNum.PUBLISH.getIndex());
            map.put("types", new int[] { channel.getIndex() });
            map.put("group", 0);
            List<CountData> datas = reckonMapper.reckonChannelNum(map);
            if (datasNotEmpty(datas)) {
                isNull = false;
            }
            setNumZero(datas, dates, 1);
            String field = TextContants.NEWS_PUBLISH_NUM;
            switch (channel) {
            case NEWS:
                field = TextContants.NEWS_PUBLISH_NUM;
                break;
            case BBS:
                field = TextContants.BBS_PUBLISH_NUM;
                break;
            case WEIXIN:
                field = TextContants.WEIXIN_PUBLISH_NUM;
                break;
            case WEIBO:
                field = TextContants.WEIBO_PUBLISH_NUM;
                break;
            default:
                break;
            }
            text = text.replace(field, datas.get(0).getFirstNum() + "");
        }
        if (isNull) {
            text = "";
        } else {
            text = text.replace(TextContants.GROUP_NAME, groupName);
        }
        moduleData.setText(text);

    }

    /**
     * 新闻舆情概述-1
     */
    private void summaryNews(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        List<Long> ids = ReportUtils.buildIdsODay(dates, plans, Channel.NEWS.getIndex());
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("num", CountNum.PUBLISH.getIndex());
        map.put("types", new int[] { Channel.NEWS.getIndex() });
        map.put("group", 0);
        String text = moduleData.getText();
        List<CountData> datas = reckonMapper.reckonChannelNum(map);
        CountData countData = null;
        if (datas.size() == 0) {
            countData = new CountData();
            countData.setFirstNum(0);
        } else {
            countData = datas.get(0);
        }
        Map<String, Object> map1 = new HashMap<>();
        map1.put("ids", ids);
        map1.put("limit", 10);
        List<RankData> datas1 = reckonMapper.reckonNewsRank(map1);

        List<Long> ids1 = ReportUtils.buildIdsODay(dates, plans, ReportUtils.RANK_NUMS);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("ids", ids1);
        map2.put("limit", 10);
        map2.put("num", CountNum.ORIGINAL_REPRINT_PUBLISH.getIndex());
        map2.put("order", 3);
        List<CountData> datas2 = reckonMapper.reckonDomainCountNum(map2);
        if (datasNotEmpty(datas)) {
            String titles = "";
            String mediaNames = "";
            for (int i = 0; i < datas1.size(); i++) {
                RankData rankData = datas1.get(i);
                titles += ("《" + rankData.getTitle() + "》");

            }
            for (int i = 0; i < datas2.size(); i++) {
                CountData countData2 = datas2.get(i);
                if (StringUtils.isNotBlank(countData2.getSourceName())) {
                    mediaNames += countData2.getSourceName();
                    if (i < datas2.size() - 1) {
                        mediaNames += "、";
                    }
                }
            }
            if (mediaNames.endsWith("、")) {
                mediaNames = mediaNames.substring(0, mediaNames.length() - 1);
            }
            text = text.replace(TextContants.GROUP_NAME, groupName)
                    .replace(TextContants.NEWS_PUBLISH_NUM, countData.getFirstNum() + "")
                    .replace(TextContants.DATA_NAME, titles).replace(TextContants.MEDIA_NAME, mediaNames);
            moduleData.setText(text);
        } else {
            moduleData.setText("");
        }
    }

    /**
     * 总体敏感信息占比-4
     */
    private void sensitive(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        Map<String, List<PlanItem>> planMap = new TreeMap<>();
        planMap.put("敏感信息", new ArrayList<PlanItem>());
        planMap.put("非敏感信息", new ArrayList<PlanItem>());
        boolean isNull = true;
        // 区分敏感和非敏感方案
        for (PlanItem planItem : plans) {
            if (planItem.getPlanSensitive() == 1) {
                planMap.get("敏感信息").add(planItem);
            } else {
                planMap.get("非敏感信息").add(planItem);
            }
        }
        for (Map.Entry<String, List<PlanItem>> entry : planMap.entrySet()) {
            // 获取ID
            List<Long> ids = ReportUtils.buildIdsODay(dates, entry.getValue(), 1);
            // 统计数据
            Map<String, Object> map = new HashMap<>();
            map.put("ids", ids);
            map.put("num", CountNum.ORIGINAL.getIndex());
            map.put("group", 0);
            map.put("time", 0);
            List<CountData> datas = reckonMapper.reckonPlanCountNum(map);
            if (datasNotEmpty(datas)) {
                isNull = false;
            }
            if (datas == null || datas.isEmpty() || datas.get(0) == null) {
                datas = new ArrayList<>();
                CountData countData = new CountData();
                countData.setFirstNum(0);
                datas.add(0, countData);
            }
            // 装入返回数据
            moduleData.addDataList(entry.getKey(), datas);
        }
        if (isNull) {
            moduleData.clearDataList();
            moduleData.setText("");
        }
    }

    /**
     * 新闻敏感信息占比-5
     */
    private void sensitiveNews(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        boolean isNull = true;
        Map<String, List<PlanItem>> planMap = new TreeMap<>();
        planMap.put("敏感信息", new ArrayList<PlanItem>());
        planMap.put("非敏感信息", new ArrayList<PlanItem>());
        // 区分敏感和非敏感方案
        for (PlanItem planItem : plans) {
            if (planItem.getPlanSensitive() == 1) {
                planMap.get("敏感信息").add(planItem);
            } else {
                planMap.get("非敏感信息").add(planItem);
            }
        }
        for (Map.Entry<String, List<PlanItem>> entry : planMap.entrySet()) {
            // 获取ID
            List<Long> ids = ReportUtils.buildIdsODay(dates, entry.getValue(), 1);
            // 统计数据
            Map<String, Object> map = new HashMap<>();
            map.put("ids", ids);
            map.put("num", CountNum.ORIGINAL.getIndex());
            map.put("group", 0);
            List<CountData> datas = reckonMapper.reckonPlanCountNum(map);
            if (datasNotEmpty(datas)) {
                isNull = false;
            }
            if (datas == null || datas.isEmpty() || datas.get(0) == null) {
                datas = new ArrayList<>();
                CountData countData = new CountData();
                countData.setFirstNum(0);
                datas.add(0, countData);
            }
            // 装入返回数据
            moduleData.addDataList(entry.getKey(), datas);
        }
        if (isNull) {
            moduleData.clearDataList();
            moduleData.setText("");
        }
    }

    /**
     * 自媒体敏感信息对比图
     */
    private void sensitiveMedias(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        // 自媒体渠道类型
        int[] mediaType = new int[] { Channel.BBS.getIndex(), Channel.WEIBO.getIndex(), Channel.WEIXIN.getIndex() };
        boolean isNull = true;
        Map<String, List<PlanItem>> planMap = new TreeMap<>();
        planMap.put("敏感信息", new ArrayList<PlanItem>());
        planMap.put("非敏感信息", new ArrayList<PlanItem>());
        // 区分敏感和非敏感方案
        for (PlanItem planItem : plans) {
            if (planItem.getPlanSensitive() == 1) {
                planMap.get("敏感信息").add(planItem);
            } else {
                planMap.get("非敏感信息").add(planItem);
            }
        }
        for (Map.Entry<String, List<PlanItem>> entry : planMap.entrySet()) {
            // 获取ID
            List<Long> ids = ReportUtils.buildIdsODay(dates, entry.getValue(), mediaType);
            // 统计数据
            Map<String, Object> map = new HashMap<>();
            map.put("ids", ids);
            map.put("num", CountNum.ORIGINAL.getIndex());
            map.put("types", mediaType);
            map.put("group", 1);
            List<CountData> datas = reckonMapper.reckonChannelNum(map);
            if (datasNotEmpty(datas)) {
                isNull = false;
            }
            setNumZero(datas, dates, 1);
            Collections.sort(datas);
            // 装入返回数据
            moduleData.addDataList(entry.getKey(), datas);
        }
        if (isNull) {
            moduleData.clearDataList();
            moduleData.setText("");
        }
    }

    /**
     * 敏感信息排行
     */
    private void sensitiveInfoRank(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        // 筛选敏感方案
        List<PlanItem> plans1 = new ArrayList<>();
        for (PlanItem planItem : plans) {
            if (planItem.getPlanSensitive() == 1) {
                plans1.add(planItem);
            }
        }
        List<Long> ids = ReportUtils.buildIdsODay(dates, plans1, ReportUtils.RANK_NUMS);
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("limit", 10);
        List<RankData> datas = reckonMapper.reckonPlanRank(map);
        if (datas.size() > 0 && datas.get(0) != null) {
            moduleData.addDataList(moduleData.getTitle(), datas);
        }
        // 加入表格标题 的字段说明
        if (moduleData.getTypeId() == 2) {
            moduleData
                    .addTableField(
                            new TableField("序号", null, moduleData.getTitle(), 0.1F, null, null, true, null, true))
                    .addTableField(new TableField("标题", TableField.TITLE, moduleData.getTitle(), 0.4F, TableField.URL,
                            null, false, null, false))
                    .addTableField(new TableField("作者", TableField.AUTHOR, moduleData.getTitle(), 0.15F, null, null,
                            false, null, false))
                    .addTableField(new TableField("来源", TableField.SOURCE_NAME, moduleData.getTitle(), 0.15F, null,
                            null, false, null, false))
                    .addTableField(new TableField("发布时间", TableField.TIME, moduleData.getTitle(), 0.2F, null, null,
                            true, null, false));
        }
    }

    /**
     * 媒体发稿top10 - 6
     */
    private void mediaTop10(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        List<Long> ids = null;
        int cycleType = Cycle.MONTH.getCode();
        if (dates.size() <= 1) {
            cycleType = Cycle.DAY.getCode();
        } else if (dates.size() <= 7) {
            cycleType = Cycle.WEEK.getCode();
        } else if (dates.size() <= 31) {
            cycleType = Cycle.MONTH.getCode();
        }
        dates = dates.subList(0, 1);
        ids = ReportUtils.buildIdsO(dates, plans, cycleType, ReportUtils.RANK_NUMS);
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("limit", 10);
        map.put("num", CountNum.ORIGINAL.getIndex());
        List<CountData> datas = reckonMapper.reckonDomainCountNum(map);
        if (datasNotEmpty(datas)) {
            moduleData.addDataList(moduleData.getTitle(), datas);
        } else {
            moduleData.setText("");
            moduleData.clearDataList();
        }
    }

    /**
     * 新闻排行 - 7
     */
    private void rankNews(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        List<Long> ids = ReportUtils.buildIdsODay(dates, plans, ReportUtils.RANK_NUMS);
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("limit", 10);
        List<RankData> datas = reckonMapper.reckonNewsRank(map);
        if (datas.size() > 0) {
            moduleData.addDataList(moduleData.getTitle(), datas);
        } else {
            moduleData.setText("");
            moduleData.clearDataList();
        }
        // 加入表格标题 的字段说明
        if (moduleData.getTypeId() == 2) {
            moduleData
                    .addTableField(
                            new TableField("序号", null, moduleData.getTitle(), 0.1F, null, null, true, null, true))
                    .addTableField(new TableField("首发媒体", TableField.SOURCE_NAME, moduleData.getTitle(), 0.15F, null,
                            null, false, null, false))
                    .addTableField(new TableField("标题", TableField.TITLE, moduleData.getTitle(), 0.3F, TableField.URL,
                            null, false, null, false))
                    .addTableField(new TableField("作者", TableField.AUTHOR, moduleData.getTitle(), 0.15F, null, null,
                            false, null, false))
                    .addTableField(new TableField("转载量", TableField.FIRST_NUM, moduleData.getTitle(), 0.1F, null, null,
                            true, null, false))
                    .addTableField(new TableField("发布时间", TableField.TIME, moduleData.getTitle(), 0.2F, null, null,
                            true, null, false));
        }
    }

    /**
     * 舆情信息对比 -8
     */
    private void compareOpinion(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        boolean isNull = true;
        for (PlanItem planItem : plans) {
            List<PlanItem> planItems = new ArrayList<>();
            planItems.add(planItem);
            List<Long> ids = ReportUtils.buildIdsODay(dates, planItems, 1);
            Map<String, Object> map = new HashMap<>();
            map.put("ids", ids);
            map.put("num", CountNum.PUBLISH.getIndex());
            map.put("group", 1);
            map.put("time", 1);
            List<CountData> datas = reckonMapper.reckonPlanCountNum(map);
            if (datasNotEmpty(datas)) {
                isNull = false;
            }
            setNumZero(datas, dates, 1);
            // 按照时间排序
            Collections.sort(datas);
            // 加入数据到返回的列表中
            moduleData.addDataList(planItem.getPlanName(), datas);
        }
        // 数据为空时清除已加入的无效数据列表
        if (isNull) {
            moduleData.setText("");
            moduleData.clearDataList();
        }
    }

    /**
     * 一周数据总览图 -9
     */
    private void weekDataT(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        Channel[] channels = new Channel[] { Channel.NEWS, Channel.BBS, Channel.WEIXIN, Channel.WEIBO };
        boolean isNull = true;
        // 加入表格标题 的字段说明
        if (moduleData.getTypeId() == 2) {
            moduleData.addTableField(new TableField("", TableField.TIME, Channel.NEWS.getName(), 0.2F, null, "MM月dd日",
                    true, null, false));
        }
        for (Channel channel : channels) {
            List<Long> ids = ReportUtils.buildIdsODay(dates, plans, channel.getIndex());
            Map<String, Object> map = new HashMap<>();
            map.put("ids", ids);
            map.put("num", CountNum.ORIGINAL.getIndex());
            map.put("types", new int[] { channel.getIndex() });
            map.put("group", 1);
            List<CountData> datas = reckonMapper.reckonChannelNum(map);
            if (datasNotEmpty(datas)) {
                isNull = false;
            }
            setNumZero(datas, dates, 1);
            // 按照时间排序
            Collections.sort(datas);
            // 加入数据到返回的列表中
            moduleData.addDataList(channel.getName(), datas);
            if (moduleData.getTypeId() == 2) {
                moduleData.addTableField(new TableField(channel.getName(), TableField.FIRST_NUM, channel.getName(),
                        0.2F, null, null, true, null, false));
            }
            moduleData.setPullByList(false);
        }
        // 数据为空时清除已加入的无效数据列表
        if (isNull) {
            moduleData.setText("");
            moduleData.clearDataList();
        }
    }

    /**
     * 一周数据总览表 -10
     */
    private void weekDataB(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        // 直接调用一周数据总览的统计方法
        weekDataT(moduleData, plans, groupName, dates);
    }

    /**
     * 新闻数据-15
     */
    private void dataNews(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        // 组装文字
        List<Long> ids1 = ReportUtils.buildIdsODay(dates, plans, Channel.NEWS.getIndex());
        Map<String, Object> map1 = new HashMap<>();
        map1.put("ids", ids1);
        map1.put("num", CountNum.ORIGINAL_REPRINT_PUBLISH.getIndex());
        map1.put("types", new int[] { Channel.NEWS.getIndex() });
        map1.put("group", 0);
        List<CountData> newsData = reckonMapper.reckonChannelNum(map1);
        setNumZero(newsData, dates, 3);
        // 查询列表
        List<Long> ids2 = ReportUtils.buildIdsODay(dates, plans, ReportUtils.RANK_NUMS);
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids2);
        map.put("limit", 10);
        List<RankData> datas = reckonMapper.reckonNewsRank(map);
        if (datas != null && datas.size() > 0) {
            moduleData.setText(moduleData.getText().replace(TextContants.GROUP_NAME, groupName)
                    .replace(TextContants.NEWS_ORIGINAL_NUM, newsData.get(0).getFirstNum() + "")
                    .replace(TextContants.NEWS_REPRINT_NUM, newsData.get(0).getSecondNum() + "")
                    .replace(TextContants.NEWS_PUBLISH_NUM, newsData.get(0).getThirdNum() + ""));
            moduleData.addDataList(moduleData.getTitle(), datas);
        } else {
            moduleData.setText("");
        }
        // 加入表格标题 的字段说明
        if (moduleData.getTypeId() == 2) {
            moduleData
                    .addTableField(
                            new TableField("序号", null, moduleData.getTitle(), 0.1F, null, null, true, null, true))
                    .addTableField(new TableField("媒体名称", TableField.SOURCE_NAME, moduleData.getTitle(), 0.15F, null,
                            null, false, null, false))
                    .addTableField(new TableField("标题", TableField.TITLE, moduleData.getTitle(), 0.4F, TableField.URL,
                            null, false, null, false))
                    .addTableField(new TableField("转载量", TableField.FIRST_NUM, moduleData.getTitle(), 0.15F, null, null,
                            true, null, false))
                    .addTableField(new TableField("发布时间", TableField.TIME, moduleData.getTitle(), 0.2F, null, null,
                            true, null, false));
        }
    }

    /**
     * 新闻数据明细 -16
     */
    private void dataNewsDetail(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        List<Long> ids = ReportUtils.buildIdsODay(dates, plans, ReportUtils.RANK_NUMS);
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("limit", 10);
        List<RankData> datas = reckonMapper.reckonNewsRank(map);
        MongoHelper mongoHelper = MongoHelper.getInstance();
        if (datas != null && datas.size() > 0 && datas.get(0) != null) {
            for (RankData rankData : datas) {
                InformationDistribute distribute = mongoHelper.queryContentBy_Id(InformationDistribute.class,
                        rankData.getDataId());
                String content = "";
                if (StringUtils.isNotBlank(distribute.getSummary())) {
                    content = ReportUtils.getReplaceAll(distribute.getSummary(), "<.*?>|\\s*|\t|\r|\n", "");
                } else if (StringUtils.isNotBlank(distribute.getContent())) {
                    content = ReportUtils.getReplaceAll(distribute.getContent(), "<.*?>|\\s*|\t|\r|\n", "");
                    content = content.length() <= 100 ? content : content.substring(0, 100);
                }
                rankData.setContent(content);
            }
            moduleData.setText(moduleData.getText().replace(TextContants.GROUP_NAME, groupName)
                    .replace(TextContants.PLAN_NAME, getPlanNameStr(plans)));
            moduleData.addDataList(moduleData.getTitle(), datas);
            moduleData
                    .addTableField(new TableField("", TableField.TITLE, moduleData.getTitle(), 1F, null, null, true,
                            "、《%s》", true))
                    .addTableField(new TableField("", TableField.CONTENT, moduleData.getTitle(), 1F, null, null, true,
                            "摘要：%s...", false));

        } else {
            moduleData.setText("");
            moduleData.clearDataList();
        }
    }
    
    /**
     * 微信数据-17
     */
    private void dataWeixin(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        // 组装文字
        List<Long> ids1 = ReportUtils.buildIdsODay(dates, plans, Channel.WEIXIN.getIndex());
        Map<String, Object> map1 = new HashMap<>();
        map1.put("ids", ids1);
        map1.put("num", CountNum.PUBLISH.getIndex());
        map1.put("types", new int[] { Channel.WEIXIN.getIndex() });
        map1.put("group", 0);
        List<CountData> newsData = reckonMapper.reckonChannelNum(map1);
        setNumZero(newsData, dates, 1);

        // 查询列表
        List<Long> ids2 = ReportUtils.buildIdsODay(dates, plans, ReportUtils.RANK_NUMS);
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids2);
        map.put("limit", 10);
        List<RankData> datas = reckonMapper.reckonWeixinRank(map);
        if (datas != null && datas.size() > 0 && datas.get(0) != null) {
            moduleData.setText(moduleData.getText().replace(TextContants.GROUP_NAME, groupName)
                    .replace(TextContants.WEIXIN_PUBLISH_NUM, newsData.get(0).getFirstNum() + "")
                    .replace(TextContants.PLAN_NAME, getPlanNameStr(plans)));
            moduleData.addDataList(moduleData.getTitle(), datas);
        } else {
            moduleData.setText("");
        }
        // 加入表格标题 的字段说明
        if (moduleData.getTypeId() == 2) {
            moduleData
                    .addTableField(
                            new TableField("序号", null, moduleData.getTitle(), 0.1F, null, null, true, null, true))
                    .addTableField(new TableField("公众号", TableField.AUTHOR, moduleData.getTitle(), 0.3F, null, null,
                            false, null, false))
                    .addTableField(new TableField("标题", TableField.TITLE, moduleData.getTitle(), 0.4F, TableField.URL,
                            null, false, null, false))
                    .addTableField(new TableField("发布时间", TableField.TIME, moduleData.getTitle(), 0.2F, null, null,
                            true, null, false));
        }
    }

    

    /**
     * 微博数据-18
     */
    private void dataWeibo(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        // 组装文字
        List<Long> ids1 = ReportUtils.buildIdsODay(dates, plans, Channel.WEIBO.getIndex());
        Map<String, Object> map1 = new HashMap<>();
        map1.put("ids", ids1);
        map1.put("num", CountNum.PUBLISH.getIndex());
        map1.put("types", new int[] { Channel.WEIBO.getIndex() });
        map1.put("group", 0);
        List<CountData> newsData = reckonMapper.reckonChannelNum(map1);
        setNumZero(newsData, dates, 1);

        // 查询列表
        List<Long> ids2 = ReportUtils.buildIdsODay(dates, plans, ReportUtils.RANK_NUMS);
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids2);
        map.put("limit", 10);
        List<RankData> datas = reckonMapper.reckonWeiboRank(map);
        if (datas != null && datas.size() > 0 && datas.get(0) != null) {
            moduleData.setText(moduleData.getText().replace(TextContants.GROUP_NAME, groupName)
                    .replace(TextContants.WEIBO_PUBLISH_NUM, newsData.get(0).getFirstNum() + "")
                    .replace(TextContants.PLAN_NAME, getPlanNameStr(plans)));
            moduleData.addDataList(moduleData.getTitle(), datas);
        } else {
            moduleData.setText("");
        }
        // 加入表格标题 的字段说明
        if (moduleData.getTypeId() == 2) {
            moduleData
                    .addTableField(
                            new TableField("序号", null, moduleData.getTitle(), 0.1F, null, null, true, null, true))
                    .addTableField(new TableField("微博名称", TableField.AUTHOR, moduleData.getTitle(), 0.3F, null, null,
                            false, null, false))
                    .addTableField(new TableField("摘要", TableField.TITLE, moduleData.getTitle(), 0.4F, TableField.URL,
                            null, false, null, false))
                    .addTableField(new TableField("发布时间", TableField.TIME, moduleData.getTitle(), 0.2F, null, null,
                            true, null, false));
        }
    }

    /**
     * 论坛数据-19
     */
    private void dataBbs(ModuleData moduleData, List<PlanItem> plans, String groupName, List<Date> dates) {
        // 组装文字
        List<Long> ids1 = ReportUtils.buildIdsODay(dates, plans, Channel.BBS.getIndex());
        Map<String, Object> map1 = new HashMap<>();
        map1.put("ids", ids1);
        map1.put("num", CountNum.PUBLISH.getIndex());
        map1.put("types", new int[] { Channel.BBS.getIndex() });
        map1.put("group", 0);
        List<CountData> newsData = reckonMapper.reckonChannelNum(map1);
        setNumZero(newsData, dates, 1);

        // 查询列表
        List<Long> ids2 = ReportUtils.buildIdsODay(dates, plans, ReportUtils.RANK_NUMS);
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids2);
        map.put("limit", 10);
        List<RankData> datas = reckonMapper.reckonBbsRank(map);
        if (datas != null && datas.size() > 0 && datas.get(0) != null) {
            moduleData.setText(moduleData.getText().replace(TextContants.GROUP_NAME, groupName)
                    .replace(TextContants.BBS_PUBLISH_NUM, newsData.get(0).getFirstNum() + "")
                    .replace(TextContants.PLAN_NAME, getPlanNameStr(plans)));
            moduleData.addDataList(moduleData.getTitle(), datas);
        } else {
            moduleData.setText("");
        }
        // 加入表格标题 的字段说明
        if (moduleData.getTypeId() == 2) {
            moduleData
                    .addTableField(
                            new TableField("序号", null, moduleData.getTitle(), 0.1F, null, null, true, null, true))
                    .addTableField(new TableField("论坛名称", TableField.SOURCE_NAME, moduleData.getTitle(), 0.2F, null,
                            null, false, null, false))
                    .addTableField(new TableField("标题", TableField.TITLE, moduleData.getTitle(), 0.5F, TableField.URL,
                            null, false, null, false))
                    .addTableField(new TableField("发布时间", TableField.TIME, moduleData.getTitle(), 0.2F, null, null,
                            true, null, false));
        }

    }

    /**
     * 判断未读取到数据时设置Num为0的列表
     */
    private void setNumZero(List<CountData> datas, List<Date> dates, int num) {
        if (datas.size() > 0 && datas.get(0) == null) {
            datas.clear();
        }
        if (datas.size() < dates.size()) {
            for (Date date : dates) {
                CountData countData = new CountData();
                countData.setTime(date);
                if (!datas.contains(countData)) {
                    if (num == 1) {
                        countData.setFirstNum(0);
                    } else if (num == 2) {
                        countData.setFirstNum(0);
                        countData.setSecondNum(0);
                    } else if (num == 3) {
                        countData.setFirstNum(0);
                        countData.setSecondNum(0);
                        countData.setThirdNum(0);
                    }
                    datas.add(countData);
                }
            }
        }
    }

    /**
     * 拼接方案名称
     */
    private String getPlanNameStr(List<PlanItem> plans) {
        StringBuilder planNames = new StringBuilder();
        for (int i = 0; i < plans.size(); i++) {
            if (StringUtils.isBlank(plans.get(i).getPlanName())) {
                continue;
            }
            planNames.append(plans.get(i).getPlanName());
            if (i < plans.size() - 1) {
                planNames.append("、");
            }
        }
        String names = planNames.toString();
        if (names.endsWith("、")) {
            names = names.substring(0, names.length() - 1);
        }
        return names;
    }

    /**
     * 判断数据列表的信息不为空
     * 
     * @param datas
     * @return 为空返回false,不为空返回true
     */
    private boolean datasNotEmpty(List<? extends BaseData> datas) {
        if (datas == null || datas.isEmpty()) {
            return false;
        }
        for (BaseData baseData : datas) {
            if (baseData != null && !baseData.isEmpty()) {
                return true;
            }
        }
        return false;
    }

}
